package com.javasm.aftersales.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.javasm.aftersales.SaleReturn;
import com.javasm.aftersales.SaleReturnService;
import com.javasm.aftersales.SalesReturnCommodity;
import com.javasm.aftersales.SalesReturnCommodityService;
import com.javasm.aftersales.dao.SaleReturnDao;
import com.javasm.commons.util.Constants;
import com.javasm.commons.util.ServletUtils;
import com.javasm.gg.Goods;
import com.javasm.process.ProcessService;
import com.javasm.sale.Order;
import com.javasm.sale.service.OrderService;
import com.javasm.sys.CurrentLogin;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SaleReturnServiceImpl extends ServiceImpl<SaleReturnDao, SaleReturn> implements SaleReturnService {

    @Resource
    private ProcessService processService;
    @Resource
    private SalesReturnCommodityService salesReturnCommodityService;
    @Resource
    private OrderService orderService;

    @Transactional
    @Override
    public boolean saveAndSubmit(SaleReturn saleReturn) {
        saleReturn.setCreateId(CurrentLogin.getLoginUser().getUserId()+"");
        saleReturn.setCreateUsername(CurrentLogin.getLoginUser().getUserName());
        //保存申请单,状态为审核中
        save(saleReturn);

        List<SalesReturnCommodity> salesReturnCommodities = new ArrayList<>();
        for (Goods commodity : saleReturn.getCommodityList()) {
            SalesReturnCommodity salesReturnCommodity = new SalesReturnCommodity();
            salesReturnCommodity.setSaleReturnId(saleReturn.getSaleReturnId());
            salesReturnCommodity.setComId(commodity.getId());
//            salesReturnCommodity.setImea(commodity.getImea());
            salesReturnCommodities.add(salesReturnCommodity);
        }
        boolean b = salesReturnCommodityService.saveBatch(salesReturnCommodities);
        //开始审核,查询审核记录表,找到第一个审核节点,把相关数据保存到审核记录表中
        processService.startProcess(Constants.SALES_RETURN_PROCESS, saleReturn.getSaleReturnId());

        //4.修改订单表的状态为售后中
        UpdateWrapper<Order> orderUpdateWrapper = new UpdateWrapper<>();
        orderUpdateWrapper.set("status", Constants.AFTER_SALE);
        orderUpdateWrapper.eq("order_id",saleReturn.getOrderId());
        orderService.update(orderUpdateWrapper);

        return true;
    }

    @Override
    public List<SaleReturn> listSaleReturnAndOrder(SaleReturn saleReturn) {
        List<String> list;
        if  (Constants.APPROVE_ING.equals(saleReturn.getStatus())){
            list =  processService.listTodoApplyIds(Constants.SALES_RETURN_PROCESS, CurrentLogin.getLoginUserName());
        }else{
            //获取要查询的 退货单id
            list =  processService.listdoApplyIds(Constants.SALES_RETURN_PROCESS, CurrentLogin.getLoginUserName(), saleReturn.getStatus());
        }
        if (list == null || list.size()==0) return null;

        //再查询
        //获取分页参数,并PageHelper.startPage
        Integer pageNum = ServletUtils.getParameter(Constants.PAGE_NUM , 1);
        Integer pageSize = ServletUtils.getParameter(Constants.PAGE_SIZE, 10);
        PageHelper.startPage(pageNum,pageSize);
        List<SaleReturn> saleReturns = baseMapper.listBySaleReturnIds(list, saleReturn);
        return saleReturns;
    }

    @Override
    public void updateOrderStatusById(String saleReturnId, String status) {
        QueryWrapper<SaleReturn> qw = new QueryWrapper<>();
        qw.select("order_id").eq("sale_return_id",saleReturnId);
        SaleReturn saleReturn = baseMapper.selectOne(qw);

        Order order = new Order();
        order.setOrderId(saleReturn.getOrderId());
        order.setStatus(status);
        orderService.updateById(order);
    }
}

